package com.group3.admin.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.group3.admin.service.IAMerchantService;
import com.group3.admin.service.impl.AMerchantServiceImpl;
import com.group3.entity.AMerchant;


/**
 * Servlet implementation class MListServlet
 * 处理A端看到的主界面  返回mlist.jsp视图
 * 	主界面显示所有商户信息 和 对应商户审核状态及黑名单状态的操作
 * 	可执行的操作 ： 
 * 		根据type 审核状态 
 * 			a) 审核中 assessing : 编辑type （assessing--> assessed 或者 assessing --> reject) 单向变动 
 * 			b) 驳回 reject : 不做操作
 * 			c) 审核通过 assessed : 拉入黑名单
 * 		根据status 黑名单状态
 * 			a) 审核通过assessed 且 status==WHITE : 拉黑
 * 			b) 审核通过 且 status == black ： 拉白
 * 
 *  可能接受的请求
 *  1. 按type （审核状态）分组显示Amerchant 
 *  2. 
 */
//@WebServlet("/MListServlet")
public class MListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IAMerchantService iAMerchantService = new AMerchantServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置每次读取时的时间戳
		request.getSession().setAttribute("readMlistTime", new Date());
		
		String type = request.getParameter("type");
		System.out.println("MListServlet request type : " + type);
		List<AMerchant> listAMerchant = null;
		if (type == null || "ALL".equals(type)) {		// 返回全部Amerchant
			listAMerchant = iAMerchantService.listAllAMerchant();			
		} else {	// 返回指定type的Amerchant
			listAMerchant = iAMerchantService.queryAMlistByType(type);			
		}
		response.setContentType("application/json;charset=utf-8");
		String jsonString = JSON.toJSONString(listAMerchant, true);
		response.getWriter().append(jsonString);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


